package com.kayo.specimen.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kayo.specimen.annotion.JsonParam;
import com.kayo.specimen.annotion.PagingParam;
import com.kayo.specimen.model.JsonResult;
import com.kayo.specimen.model.Menu;
import com.kayo.specimen.model.view.MenuView;
import com.kayo.specimen.service.MenuService;
import com.kayo.specimen.service.UserService;
import com.kayo.specimen.utility.PagingInfo;
import com.kayo.specimen.utility.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotEmpty;
import java.util.List;

import static com.kayo.specimen.model.JsonResult.STATUS_SUCCESS;

@RequiresPermissions("菜单")
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

    @RequestMapping({"","/index"})
    public ModelAndView index() {
        return new ModelAndView("/views/menu/list");
    }

    @Cacheable(value = "menu",keyGenerator = "myCacheKeyGenerator")
    @RequestMapping("/list")
    public Object list(String id, String name, String url, String privilege, String category, @PagingParam PagingInfo pagingInfo) {
        PageHelper.startPage(pagingInfo.getPage(), pagingInfo.getLimit());
        List<MenuView> menus = menuService.getMenus(id, name, url, privilege, category, pagingInfo.getOrderByClause());
        PageInfo<MenuView> result = new PageInfo<>(menus);
        return JsonResult.success(result);
    }

    @RequiresPermissions("更新菜单")
    @RequestMapping("/add")
    public ModelAndView add(){
        Menu menu=new Menu();
        ModelAndView modelAndView = new ModelAndView("/views/menu/edit");
        modelAndView.addObject("menu",menu);
        return modelAndView;
    }

    @RequiresPermissions("更新菜单")
    @RequestMapping("/edit")
    public ModelAndView edit(@NotEmpty(message = "id不能为空")String id){
        List<MenuView> menus=menuService.getMenus(id,null,null,null,null,null);
        ModelAndView modelAndView = new ModelAndView("/views/menu/edit");
        modelAndView.addObject("menu",menus.get(0));
        return modelAndView;
    }

    @Caching(evict = {@CacheEvict(value = "menu",key = "#menu.id",allEntries = true)},put = {@CachePut(value = "menu",key = "#menu.id")})
    @RequiresPermissions("更新菜单")
    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    public Object update(@JsonParam Menu menu) {
        if (StringUtils.isNullOrEmpty(menu.getId())) {
            menuService.addMenus(menu);
        } else {
            menuService.updateMenus(menu);
        }

        return JsonResult.success(menu);
    }

    @CacheEvict(value = "menu",key = "#id",allEntries = true)
    @RequiresPermissions("删除菜单")
    @RequestMapping("/delete")
    public Object delete(@NotEmpty(message = "id不能为空") @JsonParam String[] id) {
        menuService.removeMenus(id);
        return JsonResult.success();
    }
}
